package com.tubeonfire.blobstore;

import java.io.IOException;
import java.util.Calendar;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.appengine.api.blobstore.BlobKey;
import com.google.appengine.api.images.ImagesService;
import com.google.appengine.api.images.ImagesServiceFactory;

@SuppressWarnings("serial")
public class ImageUrlProcess extends HttpServlet {

	public void doGet(HttpServletRequest req, HttpServletResponse res)
			throws IOException {
		try {
			Calendar cal = Calendar.getInstance();
			cal.add(Calendar.DATE, 10);
			res.setHeader("Cache-Control", "public, max-age=86400");
			res.setHeader("Expires", cal.getTime().toString());
			if (req.getParameter("key") != null
					&& req.getParameter("size") != null) {
				BlobKey blobKey = new BlobKey(req.getParameter("key"));
				ImagesService imagesService = ImagesServiceFactory
						.getImagesService();
				res.sendRedirect(imagesService.getServingUrl(blobKey) + "=s"
						+ req.getParameter("size"));
			} else {
				res.getWriter().print("/image/tube.png");
			}
		} catch (Exception e) {
			e.printStackTrace();
			res.getWriter().print("/image/tube.png");
		}
	}
}
